package cn.itcast.goods.controller.admin;

import cn.itcast.anno.PreAuthorize;
import cn.itcast.goods.dto.DishDTO;
import cn.itcast.goods.dto.DishPageQueryDTO;
import cn.itcast.goods.service.DishService;
import cn.itcast.goods.vo.DishVO;
import cn.itcast.result.PageBean;
import cn.itcast.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @PreAuthorize("dish:pageQuery")
    @ApiOperation("菜品分页查询")
    public Result<PageBean> pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品分页查询：{}", dishPageQueryDTO);
        PageBean pageBean = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageBean);
    }

    /**
     * 添加菜品
     *
     * @param dishDTO
     * @return
     */
    @PostMapping
    @PreAuthorize("dish:save")
    @ApiOperation("添加菜品")
    public Result<String> save(@RequestBody DishDTO dishDTO) {
        log.info("添加菜品：{}", dishDTO);
        dishService.save(dishDTO);
        return Result.success();
    }

    /**
     * 批量删除菜品
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @PreAuthorize("dish:deleteByIds")
    @ApiOperation("批量删除菜品")
    public Result<String> deleteByIds(@RequestParam List<Long> ids) {
        log.info("批量删除菜品：{}", ids);
        dishService.deleteByIds(ids);
        return Result.success();
    }

    /**
     * 根据id查询菜品信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @PreAuthorize("dish:getById")
    @ApiOperation("根据id查询菜品信息")
    public Result<DishVO> getById(@PathVariable Long id) {
        log.info("根据id查询菜品信息：{}", id);
        DishVO dishVO = dishService.getByIdWithFlavor(id);
        return Result.success(dishVO);
    }

    /**
     * 修改菜品信息
     *
     * @param dishDTO
     * @return
     */
    @PutMapping
    @PreAuthorize("dish:update")
    @ApiOperation("修改菜品信息")
    public Result<String> update(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品信息：{}", dishDTO);
        dishService.updateWithFlavor(dishDTO);
        return Result.success();
    }

    /**
     * 停售起售菜品
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @PreAuthorize("dish:startOrStop")
    @ApiOperation("停售起售菜品")
    public Result<String> startOrStop(@PathVariable Integer status, Long id) {
        log.info("停售起售菜品：{},{}", status, id);
        dishService.startOrStop(status, id);
        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @PreAuthorize("dish:list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<DishVO>> list(Long categoryId) {
        log.info("根据分类id查询菜品：{}", categoryId);
        List<DishVO> list = dishService.list(categoryId);
        return Result.success(list);
    }

}
